//
//  LBCCategoryTopVC.m
//  喜马拉雅改良版框架
//
//  Created by dllo on 16/1/4.
//  Copyright © 2016年 蓝鸥科技. All rights reserved.
//

#import "LBCCategoryTopVC.h"

@interface LBCCategoryTopVC ()<UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong)UITableView *topTableView;
@property (nonatomic, strong)NSMutableArray *topArr;

@end

@implementation LBCCategoryTopVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    UIImage *backImage = [UIImage imageNamed:@"icon_back_h@2x.png"];
    backImage = [backImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithImage:backImage style:UIBarButtonItemStyleDone target:self action:@selector(backAction)];
    self.navigationItem.title = [_topDic objectForKey:@"title"];
    
    _topTableView = [[UITableView alloc] initWithFrame:self.view.frame style:UITableViewStylePlain];
    _topTableView.delegate = self;
    _topTableView.dataSource = self;
    [self.view addSubview:_topTableView];
    [_topTableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.right.left.bottom.mas_equalTo(@0);
        
    }];
    _topTableView.rowHeight = SCREEN_HEIGHT / 8.3375;
    
    [_topTableView registerClass:[LXHotTableViewCell class] forCellReuseIdentifier:@"LXHotTableViewCell"];
    
    _topArr = [NSMutableArray array];
    [self getData];
    
}


- (void)getData{
    NSString *urlStr = [NSString stringWithFormat:@"http://mobile.ximalaya.com/mobile/discovery/v1/rankingList/album?device=iPhone&key=%@&pageId=1&pageSize=20&position=0&title=%@", [_topDic objectForKey:@"key"], [_topDic objectForKey:@"title"]];
    [AFNetworkTool getUrl:urlStr body:nil result:HXJSON headerFile:nil success:^(id result) {
        
        NSMutableArray *array = [result objectForKey:@"list"];
        for (NSMutableDictionary *dic in array) {
            LXHotModel *hotM = [LXHotModel new];
            [hotM setValuesForKeysWithDictionary:dic];
            [_topArr addObject:hotM];
        }
        [_topTableView reloadData];
    } failure:^(NSError *error) {
        
        
    }];
    
}


-(void)backAction{
    [self.navigationController popViewControllerAnimated:YES];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _topArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *idntifier = @"LXHotTableViewCell";
    LXHotTableViewCell *topCell = [tableView dequeueReusableCellWithIdentifier:idntifier];
    // 显示cell右侧的箭头
    [topCell setAccessoryType:UITableViewCellAccessoryDisclosureIndicator];
    
    topCell.index = indexPath.row;
    topCell.cellModel = _topArr[indexPath.row];
    
    return topCell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    KBAlbumsDetailViewController *albumsDVC = [KBAlbumsDetailViewController new];
    LXHotModel *topM = _topArr[indexPath.row];
    albumsDVC.albumId = [NSString stringWithFormat:@"%ld", topM.albumId];
    [self.navigationController pushViewController:albumsDVC animated:YES];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
